<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const arr = [21, 342, 1, 3, 5, 7, 9, 34, 2, 6, 89, 3, 2, 65, 7, 3, 2]

    const findAll = function (arr, x) {
      let res = [],
        len = arr.length,
        ps = 0
      while (ps < len) {
        ps = arr.indexOf(x, ps)
        if (ps === -1) break
        res.push(ps)
        ps++
      }
      return res
    }

    console.log(findAll(arr, 3))
  </script>
</body>

</html>